package services;

import dao.StringToImg_Repository;
import models.DatabaseExecutionContext;
import play.db.jpa.JPAApi;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import utils.StringToImg;

import javax.inject.Inject;
import java.io.*;
import java.sql.Timestamp;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;

public class StringToImg_JPA implements StringToImg_Repository {


    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public StringToImg_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }




    @Override
    public CompletionStage<Boolean > StringToImg (String imgStr)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
//                byte[] data = null;
//                BASE64Encoder encoder = new BASE64Encoder();
//                String imgStr = encoder.encode(data);
                    // 对字节数组字符串进行Base64解码并生成图片
                // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理


//
//                String imgFile = "public/images/8.jpg";
//                InputStream in = null;
//                byte[] data = null;
//                // 读取图片字节数组
//                try {
//                    in = new FileInputStream(imgFile);
//                    data = new byte[in.available()];
//                    in.read(data);
//                    in.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                // 对字节数组Base64编码
//                BASE64Encoder encoder = new BASE64Encoder();
//                // 返回Base64编码过的字节数组字符串
//                System.out.println(encoder.encode(data));




                if (imgStr == null)
                { return false;}
                BASE64Decoder decoder = new BASE64Decoder();
                    try {
                        // Base64解码
                        byte[] b = decoder.decodeBuffer(imgStr);
                        for (int i = 0; i < b.length; ++i) {
                            if (b[i] < 0) {// 调整异常数据
                                b[i] += 256;
                            }
                        }
                        // 生成新的图片
                        String now = new Timestamp(System.currentTimeMillis()).toString().replace(" ", "").replace("-", "").replace(":", "").substring(0, 14);

                        String imgFilePath = "public/images/"+ now +".jpg";
                        OutputStream out = new FileOutputStream(imgFilePath);
                        out.write(b);
                        out.flush();
                        out.close();
                        File image = new File(imgFilePath);
                        if(StringToImg.isImage(image)==false){
                            System.out.println(new Timestamp(System.currentTimeMillis())+"相片格式不对"+StringToImg.isImage(image));
                            new File("public/images/"+ now +".jpg").delete();
                            return false;

                            }
                        return true;
                    } catch (Exception e) {
                        return false;
                    }


            });
        }, executionContext);
    }



}
